package cn.wolfcode.web.controller;


import cn.wolfcode.domain.MessageBoard;
import cn.wolfcode.domain.MessageReply;
import cn.wolfcode.domain.SystemDictionaryItem;
import cn.wolfcode.qo.JsonResult;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IMessageBoardService;
import cn.wolfcode.service.IMessageReplyService;
import cn.wolfcode.service.ISystemDictionaryItemService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/messageBoard")
public class MessageBoardController {
    @Autowired
    private IMessageBoardService messageBoardService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private IMessageReplyService messageReplyService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("parentList", systemDictionaryItemService.selectParentList("business"));
        return "list";
    }

    @RequestMapping("/serverList")
    public String serverList(Model model, @ModelAttribute("qo") QueryObject qo){
        PageInfo<MessageBoard> pageInfo = messageBoardService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("parentList", systemDictionaryItemService.selectParentList("business"));
        return "message/serverList";
    }

    @RequestMapping("/delete")
    public String delete(Long id) {
        if (id != null) {
           messageBoardService.delete(id);
        }
        return "redirect:/messageBoard/serverList";
    }

    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(MessageBoard messageBoard) {
        if (messageBoard.getId() == null) {
            messageBoardService.save(messageBoard);
        } else {
            messageBoardService.update(messageBoard);
        }
        return "redirect:/messageBoard/serverList";
    }

    @RequestMapping("/serverInput")
    public String serverInput(Long id, Model model) {
        if (id != null) {
            model.addAttribute("messageBoard",messageBoardService.get(id));
            model.addAttribute("count",messageBoardService.queryByCount(id));
        }
        return "message/serverInput";
    }

    @RequestMapping("/detail")
    public String input(Long id, Model model) {
        if (id != null) {
            model.addAttribute("messageBoard",messageBoardService.get(id));
            model.addAttribute("count",messageBoardService.queryByCount(id));
        }
        return "detail";
    }

    @RequestMapping("/queryByItem")
    @ResponseBody
    public List<SystemDictionaryItem> queryByItem(Long id) {
        List<SystemDictionaryItem> list = null;
        if (id != null) {
            list = systemDictionaryItemService.queryByChild(id);
        }
        return list;
    }

    @RequestMapping("/saveMessage")
    @ResponseBody
    public JsonResult saveMessage(MessageBoard messageBoard) {
        messageBoardService.saveMessage(messageBoard);
        return new JsonResult();
    }

    @RequestMapping("/saveReply")
    @ResponseBody
    public JsonResult saveReply(MessageReply messageReply,Long replyId) {
        messageReplyService.saveReply(messageReply, replyId);
        return new JsonResult();
    }
}
